// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

import org.codehaus.groovy.runtime.IOGroovyMethods

suite ("projectMV3") {
    String db = context.config.getDbNameByFile(context.file)
    sql "use ${db}"
    sql "SET experimental_enable_nereids_planner=true"
    sql "SET enable_fallback_to_original_planner=false"
    sql """ DROP TABLE IF EXISTS projectMV3; """
    // this mv rewrite would not be rewritten in RBO, so set NOT_IN_RBO explicitly
    sql "set pre_materialized_view_rewrite_strategy = NOT_IN_RBO"

    sql """
            create table projectMV3 (
                time_col dateV2, 
                empid int, 
                name varchar, 
                deptno int, 
                salary int, 
                commission int)
            partition by range (time_col) (partition p1 values less than MAXVALUE) distributed by hash(time_col) buckets 3 properties('replication_num' = '1');
        """


    sql """insert into projectMV3 values("2020-01-01",1,"a",1,1,1);"""
    sql """insert into projectMV3 values("2020-01-02",2,"b",2,2,2);"""

    def result = "null"

    createMV("create materialized view projectMV3_mv as select deptno as a1, empid as a2, name as a3 from projectMV3 order by deptno;")
    create_sync_mv(db, "projectMV3", "projectMV3_mv", "select deptno as a1, empid as a2, name as a3 from projectMV3 order by deptno;")

    sql """insert into projectMV3 values("2020-01-01",1,"a",1,1,1);"""

    sql "analyze table projectMV3 with sync;"
    sql """alter table projectMV3 modify column time_col set stats ('row_count'='3');"""
    sql """set enable_stats=false;"""

    mv_rewrite_fail("select * from projectMV3 order by empid;", "projectMV3_mv")
    order_qt_select_star "select * from projectMV3 order by empid;"

    mv_rewrite_success("select empid + 1, name from projectMV3 where deptno = 1 order by empid;", "projectMV3_mv")
    order_qt_select_mv "select empid + 1, name from projectMV3 where deptno = 1 order by empid;"

    mv_rewrite_success("select name from projectMV3 where deptno = 0 order by empid;", "projectMV3_mv")
    order_qt_select_mv2 "select name from projectMV3 where deptno -1 = 0 order by empid;"

    sql """set enable_stats=true;"""
    sql """alter table projectMV3 modify column time_col set stats ('row_count'='3');"""

    mv_rewrite_fail("select * from projectMV3 order by empid;", "projectMV3_mv")

    mv_rewrite_success("select empid + 1, name from projectMV3 where deptno = 1 order by empid;", "projectMV3_mv")

    mv_rewrite_success("select name from projectMV3 where deptno = 0 order by empid;", "projectMV3_mv")
}
